﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fmis21_10.Models;

namespace Fmis21_10.Controllers
{
    public class CardHistoryController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /CardHistory/

        public ActionResult Index()
        {
            var cardhistories = db.CardHistories.Include(c => c.Player).Include(c => c.MatchInformation);
            return View(cardhistories.ToList());
        }

        //
        // GET: /CardHistory/Details/5

        public ActionResult Details(int id = 0)
        {
            CardHistory cardhistory = db.CardHistories.Find(id);
            if (cardhistory == null)
            {
                return HttpNotFound();
            }
            return View(cardhistory);
        }

        //
        // GET: /CardHistory/Create

        public ActionResult Create()
        {
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName");
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId");
            return View();
        }

        public PartialViewResult MultiCreate()
        {
            return PartialView("MultiCreate");
        }
        //
        // POST: /CardHistory/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CardHistory cardhistory, FormCollection f)
        {
            if (ModelState.IsValid)
            {
                var iformationmatchIdArray = f.GetValues("MatchInformationId");
                var playerIdArray = f.GetValues("PlayerId");
                var timeArray = f.GetValues("Time");
                var yellowArray = f.GetValues("YellowCard");
                var redArray = f.GetValues("RedCard");
                for (int i = 0; i < playerIdArray.Count(); i++)
                {
                    cardhistory.MatchInformationId = Convert.ToInt32(iformationmatchIdArray[i]);
                    cardhistory.PlayerId = Convert.ToInt32(playerIdArray[i]);
                    cardhistory.Time = Convert.ToInt32(timeArray[i]);
                    cardhistory.YellowCard = Convert.ToInt32(yellowArray[i]);
                    cardhistory.RedCard = Convert.ToInt32(redArray[i]);
                    db.CardHistories.Add(cardhistory);
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }

            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", cardhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", cardhistory.MatchInformationId);
            return View(cardhistory);
        }

        //
        // GET: /CardHistory/Edit/5

        public ActionResult Edit(int id = 0)
        {
            CardHistory cardhistory = db.CardHistories.Find(id);
            if (cardhistory == null)
            {
                return HttpNotFound();
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", cardhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", cardhistory.MatchInformationId);
            return View(cardhistory);
        }

        //
        // POST: /CardHistory/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CardHistory cardhistory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cardhistory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PlayerId = new SelectList(db.Players, "PlayerId", "FirstName", cardhistory.PlayerId);
            ViewBag.MatchInformationId = new SelectList(db.MatchInformations, "MatchInformationId", "MatchInformationId", cardhistory.MatchInformationId);
            return View(cardhistory);
        }

        //
        // GET: /CardHistory/Delete/5

        public ActionResult Delete(int id = 0)
        {
            CardHistory cardhistory = db.CardHistories.Find(id);
            if (cardhistory == null)
            {
                return HttpNotFound();
            }
            return View(cardhistory);
        }

        //
        // POST: /CardHistory/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CardHistory cardhistory = db.CardHistories.Find(id);
            db.CardHistories.Remove(cardhistory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}